<?php
class OauthAction extends BaseAction
{
    public function qq()
    {
        $this->showBrowMes();
    }

    /**
     * 获取微博登陆连接地址
     * @author ice <iceinto@mallog.com.cn>
     */
    public function getWeiBoLoingUrl()
    {
        vendor('WeiBo.WeiBo');
        $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
        return $o->getAuthorizeURL(SITE_URL . '/oauth/registered/weibo');
    }

    /**
     * 微博登陆返回信息
     * @author ice <iceinto@mallog.com.cn>
     */
    public function weibo()
    {
        vendor('WeiBo.WeiBo');
        $token = null;
        $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_GET['code'];
            $keys['redirect_uri'] = SITE_URL . '/oauth/registered/weibo';
            try {
                $token = $o->getAccessToken('code', $keys);
            } catch (OAuthException $e) {
                dump($e->getMessage());
            }
        }

        if ($token != null) {
            $oauth = $this->checkOAuth('weibo', $token['access_token'], $token['uid']);
            $this->checkStatus($oauth);
        } else {
            $this->error('TOKEN 获取错误！');
        }
    }

    public function renren()
    {

    }

    /**
     * 查看OAuth
     * @param string $site
     * @param string $openid
     * @author ice <iceinto@mallog.com.cn>
     */
    public function checkOAuth($site, $openid, $uid)
    {
        $OAuthModel = D('OAuth');
        //1.获取 oauth 表中是否用户已经绑定
        //2.如果没有绑定 则进行绑定 或者 注册
        //3.如果已经绑定则进行登陆操作
        $oauth = $OAuthModel->getOne($site, $openid);
        if ($oauth) {
            //添加一次登陆信息
            $data = array();
            $data['itemid'] = $oauth['itemid'];
            $data['login_count'] = array('exp', 'login_count+1');
            $data['login_time'] = time();
            $oauth['openid'] = $openid; //修改标识 防止地址变化可读标识变化
            $OAuthModel->save($data);
        } else {
            //添加 oauth
            $oauth = array();
            $oauth['add_time'] = time();
            $oauth['site'] = $site;
            $oauth['openid'] = $openid;
            $data['uid'] = $uid;
            $OAuthModel->add($oauth);
        }
        return $oauth;
    }

    /**
     * 查看OAuth状态
     * @param array $oauth
     * @author ice <iceinto@mallog.com.cn>
     */
    public function checkStatus($oauth)
    {
        if ($oauth) {
            if ($oauth['uid']) {
                //如果已经绑定则进行登陆操作
                $user = M('Users')->field('id,name,email,status')->find($oauth['uid']);
                //获取用户信息
                $this->doLogin($user);
            } else {
                //如果没有绑定则进行登陆 或 注册
                if (getLoginUserInfo()) {
                    $OAuthModel = D('OAuth');
                    //自动绑定
                    $data = array();
                    $data['itemid'] = $oauth['itemid'];
                    $data['uid'] = getLoginUserInfo();
                    $data['openid'] = $oauth['openid'];
                    $rs = $OAuthModel->save($data);
                    if ($rs) {
                        $this->assign("jumpUrl", SITE_URL);
                        $this->success('自动绑定成功！');
                    } else {
                        $this->error('自动绑定失败！');
                    }
                } else {
                    $this->assign('oauth', $oauth);
                    $this->error('如果还没有摩客网账号，麻烦你先要注册帐户并且登陆，再进行绑定。');
                }
            }
        } else {
            $this->error('验证信息不能为空！');
        }
    }

    /**
     * 执行用户登陆
     * @param int $uid
     * @author ice <iceinto@mallog.com.cn>
     */
    public function doLogin($user)
    {
        if ($user && $user['id']) {
            $uid = $user['id'];
            setLoginUserInfo($uid);
            //注册成功修改，在线状态。
            setOnline($uid);
            //记录登陆日志
            loginLog($uid);
            //记录登陆次数
            addUserLoginCount($uid);
            //进行登陆积分处理
            if ($user['status'] == 2) {
                D('CreditUser')->change($uid, 1); //执行登陆积分操作
            }
            $this->assign("jumpUrl", SITE_URL);
            $this->success('登陆成功！');
        }
    }

    private function showBrowMes()
    {
        dump($_SESSION);
        dump($_COOKIE);
    }

    /**
     * 解除绑定
     */
    public function unbind()
    {
        $uid = getLoginUserInfo();
        if ($uid) {
            $type = $this->_get('type');
            $OAuthModel = D('OAuth');
            $oauth = $OAuthModel->getUserOneBind($uid, $type);
            if ($oauth) {
                $data['uid'] = 0;
                $data['itemid'] = $oauth['itemid'];
                $rs = $OAuthModel->save($data);
                if ($rs) {
                    $this->assign("jumpUrl", U('/member/setting', null, false, false));
                    $this->success('解绑成功！');
                } else {
                    $this->error('解绑失败！');
                }
            } else {
                $this->error('绑定信息不存在！');
            }
        }
    }

    /**
     * 登陆地址获取
     * @author ice <iceinto@mallog.com.cn>
     */
    public function login()
    {
        $type = $this->_get('type');
        $data = array();
        if ($type == 'weibo') {
            $data['url'] = $this->getWeiBoLoingUrl();
        } else {
        }
        $this->ajaxReturn($data, null, 1);
    }

    /**
     * 登陆回调处理
     * @author ice <iceinto@mallog.com.cn>
     */
    public function registered()
    {
        $type = $this->_get('type');
        if ($type == 'weibo') {
            $this->weibo();
        } else {
        }
    }

    /**
     * 进行绑定
     */
    public function bind()
    {
        dump($_GET);
        $type = $this->_get('type');
        if ($type == 'weibo') {
            redirect($this->getWeiBoLoingUrl());
        }
    }
}

?>